package com.briup.myblog.service.impl;
import com.briup.myblog.bean.basic.BaseConfig;
import com.briup.myblog.dao.basic.BaseConfigMapper;
import com.briup.myblog.dao.ext.BaseConfigExtMapper;
import com.briup.myblog.exception.CustomerException;
import com.briup.myblog.service.IBaseConfigService;
import com.briup.myblog.utils.JwtUtil;
import com.briup.myblog.utils.ResultStatus;
import com.briup.myblog.utils.ResultUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@Service
public class BaseConfigServiceImpl implements IBaseConfigService {
    @Autowired
    private BaseConfigMapper mapper;
    @Autowired
    private BaseConfigExtMapper extMapper;
    @Override
    public void saveConfig(BaseConfig baseConfig) {
        BaseConfig b = extMapper.selectName2(baseConfig.getConfigName());
        if (b == null) {
            extMapper.saveConfig(baseConfig);
        } else {
            throw new CustomerException("系统名称已存在");
        }
    }
    @Override
    public void updateConfig(BaseConfig baseConfig) {
        BaseConfig b = extMapper.selectName(baseConfig.getConfigName(),baseConfig.getConfigId());
        if (b == null) {
            extMapper.updateConfig(baseConfig);
        } else {
            throw new CustomerException("系统名称已存在");
        }
    }
    @Override
    public void changeConfigStatus(int id, int status) {
           List<BaseConfig> list=extMapper.selectStatus();
           int num=list.get(0).getConfigId();
           if(num==id){
               throw new CustomerException("无法全部禁用");
           }
            extMapper.changeConfigStatus(id,status);
            extMapper.updateStatus(id,status);
    }
    @Override
    public void deleteById(int id) {
        int num;
        int num2;
        num=extMapper.selectCount();
        num2=extMapper.selectConfigStatus(id);
        if(num==1){
            throw new CustomerException("该系统只剩余一个配置信息，无法删除");
        }
        else if (num2==1){
            throw new CustomerException("请禁用该启用配置信息，在尝试删除");
        }
        else {
            extMapper.deleteById(id);
        }

    }
    @Override
    public PageInfo<BaseConfig> findAllWithPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<BaseConfig> list=extMapper.selectALL();
        PageInfo<BaseConfig> pageInfo=new PageInfo<>(list);
        return pageInfo;

    }
    @Override
    public BaseConfig currentConfigYes() {
        return extMapper.currentConfigYes();
    }
}




